package com.bai.Controller;

import com.bai.Pojo.Emp;
import com.bai.Service.EmpService;
import com.bai.Utils.DtoResultUtil;
import com.bai.dto.PageLimitDto;
import com.bai.dto.PublicResponseBody;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/Emp")
public class EmpController {
    @Autowired
    private EmpService empService;

    //查询所有员工
    @GetMapping("/Emp_table")
    public PageLimitDto QueryAllEmp(String empjob, String empdep, String empname, Integer offset, Integer limit){
        PageLimitDto result =empService.QueryAll(empjob,empdep,empname,offset,limit);
        return result;
    }

    //添加员工
    @RequiresRoles(value={"hr","admin"},logical= Logical.OR)
    @GetMapping("/add")
    public PublicResponseBody add(Emp emp){
        PublicResponseBody addEmp = empService.addEmp(emp);
        if (addEmp!=null){
            if (addEmp==null){
                return DtoResultUtil.resultError();
            }
            return addEmp;
        }else {
            return DtoResultUtil.resultParamError();
        }
    }


    //删除
    @RequiresRoles(value={"hr","admin"},logical= Logical.OR)
    @GetMapping("/delete")
    public PublicResponseBody delete(Integer id){
        if (id!=null){
            PublicResponseBody result = empService.deleteEmpById(id);
            if (result==null){
                return DtoResultUtil.resultError();
            }
            return result;
        }else {
            return DtoResultUtil.resultParamError();
        }
    }

    //编辑修改
    @RequiresRoles(value={"hr","admin"},logical= Logical.OR)
    @PostMapping("/edit")
    public PublicResponseBody edit(Emp emp){
        PublicResponseBody updateEmp = empService.UpdateEmp(emp);
        if (updateEmp!=null){
            if (updateEmp==null){
                return DtoResultUtil.resultError();
            }
            return updateEmp;
        }else {
            return DtoResultUtil.resultParamError();
        }
    }

    @RequiresRoles(value={"hr","admin"},logical= Logical.OR)
    @GetMapping("/getDetail")
    public PublicResponseBody getDetail(Integer id){
        if (id!=null){
            PublicResponseBody getEmp = empService.getEmp(id);
            if (getEmp!=null){
                if (getEmp==null){
                    return DtoResultUtil.resultError();
                }
                return getEmp;
            }else {
                return DtoResultUtil.resultParamError();
            }
        }
        return DtoResultUtil.resultParamError();
    }
}
